package org.zhengquan.service;

import com.google.common.base.Preconditions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.zhengquan.common.RequestHolder;
import org.zhengquan.dao.SysAclMapper;
import org.zhengquan.dto.PageResult;
import org.zhengquan.exception.ParamException;
import org.zhengquan.pojo.SysAcl;
import org.zhengquan.util.BeanValidator;
import org.zhengquan.util.IpUtil;
import org.zhengquan.vo.AclVO;
import org.zhengquan.vo.PageQuery;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author zhengquan
 * @date 2017/11/3
 */
@Service
public class SysAclService {

    @Autowired
    private SysAclMapper sysAclMapper;

    @Autowired
    private SysLogService sysLogService;

    public void save(AclVO aclVO) {
        BeanValidator.check(aclVO);
        if (checkExit(aclVO.getAclModuleId(), aclVO.getId(), aclVO.getName())) {
            throw new ParamException("同一层级下存在相同名称的权限点");
        }
        SysAcl sysAcl = SysAcl.builder().name(aclVO.getName()).aclModuleId(aclVO.getAclModuleId()).url(aclVO.getUrl())
                .type(aclVO.getType()).state(aclVO.getState()).seq(aclVO.getSeq()).remark(aclVO.getRemark()).build();
        sysAcl.setCode(generateCode());
        sysAcl.setOperator(RequestHolder.getCurrentUser().getUsername());
        sysAcl.setOperateIp(IpUtil.getRemoteIp(RequestHolder.getCurrentRequest()));
        sysAcl.setOperateTime(new Date());
        sysAclMapper.insertSelective(sysAcl);
        sysLogService.saveAclLog(null, sysAcl);
    }

    public void update(AclVO aclVO) {
        BeanValidator.check(aclVO);
        if (checkExit(aclVO.getAclModuleId(), aclVO.getId(), aclVO.getName())) {
            throw new ParamException("同一层级下存在相同名称的权限点");
        }
        SysAcl before = sysAclMapper.selectByPrimaryKey(aclVO.getId());
        Preconditions.checkNotNull(before, "待更新的权限点不存在");

        SysAcl after = SysAcl.builder().id(aclVO.getId()).name(aclVO.getName()).aclModuleId(aclVO.getAclModuleId()).url(aclVO.getUrl())
                .type(aclVO.getType()).state(aclVO.getState()).seq(aclVO.getSeq()).remark(aclVO.getRemark()).build();
        after.setCode(generateCode());
        after.setOperator(RequestHolder.getCurrentUser().getUsername());
        after.setOperateIp(IpUtil.getRemoteIp(RequestHolder.getCurrentRequest()));
        after.setOperateTime(new Date());
        sysAclMapper.updateByPrimaryKeySelective(after);
        sysLogService.saveAclLog(before, after);
    }

    public PageResult<SysAcl> selectPageByAclModuleId(Integer aclModuleId, PageQuery pageQuery) {
        BeanValidator.check(pageQuery);
        int count = sysAclMapper.countByAclModuleId(aclModuleId);
        if (count > 0) {
            List<SysAcl> list = sysAclMapper.selectPageByAclModuleId(aclModuleId, pageQuery);
            return PageResult.<SysAcl>builder().total(count).data(list).build();
        }
        return PageResult.<SysAcl>builder().build();
    }

    private boolean checkExit(Integer aclModuleId, Integer aclId, String aclName) {
        return sysAclMapper.countByNameAndAclModuleId(aclModuleId, aclId, aclName) > 0;
    }

    private String generateCode() {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        return dateFormat.format(new Date()) + "_" + (int) Math.random() * 100;
    }
}
